{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f802e64d-4eaa-445d-a48a-1042a91bc394",
   "metadata": {
    "tags": []
   },
   "source": [
    "# 百度搜索通用问答机器人\n",
    "\n",
    "## 目标\n",
    "使用百度搜索，无需自建知识库，对用户的请求进行回答。\n",
    "\n",
    "用户可通过自定义人设来创建自己的问答机器人服务，并可通过开关配置拒答、澄清反问、重点强调、友好度提升、溯源等能力。\n",
    "\n",
    "## 准备工作\n",
    "### 平台注册\n",
    "\n",
    "1.先在appbuilder平台注册，获取token\n",
    "\n",
    "2.安装appbuilder-sdk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2939356f-61c2-42e9-9e0c-fc6729c193f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install appbuilder-sdk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ccff03b-1567-4e8b-8e1f-9a5032690406",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "#  设置环境变量\n",
    "os.environ[\"APPBUILDER_TOKEN\"] = \"...\"\n",
    "\n",
    "print(\"init done\")\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "aeb2fa55-075f-48df-a9fb-8b40d9900684",
   "metadata": {},
   "source": [
    "## 开发过程\n",
    "### 配置 RAGWithBaiduSearch 组件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41559341-fd7a-478c-a08b-1477d79e9d41",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-18T06:24:26.982459Z",
     "start_time": "2023-12-18T06:23:53.771345Z"
    }
   },
   "outputs": [],
   "source": [
    "import appbuilder\n",
    "\n",
    "# 人设指令\n",
    "instruction = appbuilder.Message(\"你是问答助手，在回答问题前需要加上“很高兴为您解答“\")\n",
    "\n",
    "# 能力开关\n",
    "reject = False # 拒答\n",
    "clarify = False # 澄清反问\n",
    "highlight = True # 重点强调\n",
    "friendly = False # 友好度提升\n",
    "cite = True # 溯源\n",
    "\n",
    "# 使用 eb-turbo-appbuilder 模型\n",
    "component = appbuilder.RAGWithBaiduSearch(model=\"eb-turbo-appbuilder\")\n",
    "query = appbuilder.Message(\"海淀区的面积是多少\")\n",
    "\n",
    "answer = component.run(\n",
    "    query, instruction=instruction, reject=reject, clarify=clarify, highlight=highlight, friendly=friendly, cite=cite, stream=False)\n",
    "\n",
    "print(f\"问答结果: {answer.content}\")\n",
    "print(f\"检索返回: {answer.extra}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5fc5bc38-6bc5-4187-a8fd-f802d77d89fa",
   "metadata": {},
   "source": [
    "### (可选) 使用 AgentBase 启动 chainlit 页面调试\n",
    "这部分代码依赖 `appbuilder-sdk[serve]`，如果没有安装，可以执行下面的命令安装："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a10737ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install appbuilder-sdk[serve]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "16a3abb2",
   "metadata": {},
   "source": [
    "执行下面的代码，会启动一个 chainlit 页面，页面地址：0.0.0.0:8091"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d514a628-5ae3-4269-aada-eaf8b21c3793",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent = appbuilder.AgentRuntime(component=component)\n",
    "# 启动 chainlit 服务\n",
    "agent.chainlit_demo(port=8091)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "16a8aa38-7a33-4e27-bca4-00900cfe1641",
   "metadata": {},
   "source": [
    "### (可选) 使用 AgentBase 启动 HTTP 服务\n",
    "这部分代码依赖 `appbuilder-sdk[serve]`，如果没有安装，可以执行下面的命令安装："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f449fcb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install appbuilder-sdk[serve]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d795f905",
   "metadata": {},
   "source": [
    "执行下面的代码，会启动一个 HTTP 服务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f45ef5f-6206-4b31-83c4-3c8eb2c86925",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent.serve(port=8092)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ccd4c48f",
   "metadata": {},
   "source": [
    "测试服务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7313e122-5199-4c90-bc6c-ad04e206ccc9",
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "curl --location 'http://0.0.0.0:8092/chat' \\\n",
    "--header 'Content-Type: application/json' \\\n",
    "--data '{\n",
    "    \"message\": \"海淀区的面积是多少\",\n",
    "    \"stream\": false\n",
    "}'"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
